package com.ext.portlet.registration.util;

import javax.portlet.ActionRequest;
import javax.servlet.http.HttpServletRequest;

import com.ext.portlet.registration.model.CollOthers;
import com.ext.portlet.registration.model.College;
import com.ext.portlet.registration.model.Mainstay;
import com.ext.portlet.registration.model.Others;
import com.ext.portlet.registration.model.Parent;
import com.ext.portlet.registration.model.School;
import com.ext.portlet.registration.model.Staff;
import com.ext.portlet.registration.model.Teacher;
import com.ext.portlet.registration.service.CollOthersLocalServiceUtil;
import com.ext.portlet.registration.service.CollegeLocalServiceUtil;
import com.ext.portlet.registration.service.MainstayLocalServiceUtil;
import com.ext.portlet.registration.service.OthersLocalServiceUtil;
import com.ext.portlet.registration.service.ParentLocalServiceUtil;
import com.ext.portlet.registration.service.SchoolLocalServiceUtil;
import com.ext.portlet.registration.service.StaffLocalServiceUtil;
import com.ext.portlet.registration.service.TeacherLocalServiceUtil;
import com.liferay.portal.DuplicateUserEmailAddressException;
import com.liferay.portal.DuplicateUserScreenNameException;
import com.liferay.portal.GroupFriendlyURLException;
import com.liferay.portal.GroupNameException;
import com.liferay.portal.NoSuchGroupException;
import com.liferay.portal.NoSuchUserException;
import com.liferay.portal.PortalException;
import com.liferay.portal.ReservedUserScreenNameException;
import com.liferay.portal.SystemException;
import com.liferay.portal.UserScreenNameException;
import com.liferay.portal.kernel.util.CharPool;
import com.liferay.portal.kernel.util.InstancePool;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.User;
import com.liferay.portal.model.impl.LayoutImpl;
import com.liferay.portal.security.auth.ScreenNameValidator;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.service.GroupServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.service.base.PrincipalBean;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.PrefsPropsUtil;
import com.liferay.portal.util.PropsKeys;
import com.liferay.portal.util.PropsValues;

public class Validation {
	
	public static void email(ActionRequest req, String emailAddress) 
			throws PortalException, SystemException{
		
		HttpServletRequest request = PortalUtil.getHttpServletRequest(req); 
		ThemeDisplay theme = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
		long companyId = theme.getCompanyId();
		
		email(companyId, emailAddress);
	}
	
	
	public static void email(long companyId, String emailAddress) 
			throws PortalException, SystemException{		
	
		User user = null;
	
		try{
				user = UserLocalServiceUtil.getUserByEmailAddress(companyId, emailAddress);
			}catch(NoSuchUserException ex){}
		
		Teacher teacher = TeacherLocalServiceUtil.getByEmail(emailAddress);		
		Parent parent = ParentLocalServiceUtil.getByEmailAddress(emailAddress);		
		School school = SchoolLocalServiceUtil.getByEmail(emailAddress); 		
		// Blossom others 
		Others others = OthersLocalServiceUtil.EmailValidation(emailAddress);
		
		CollOthers collOther = CollOthersLocalServiceUtil.EmailValidation(emailAddress);		
		Staff staff = StaffLocalServiceUtil.getByEmail(emailAddress);		
		College college = CollegeLocalServiceUtil.getByEmail(emailAddress); 
		
		Mainstay mainstay = MainstayLocalServiceUtil.EmailValidation(emailAddress);		
		
		if(Validator.isNotNull(user)||
				Validator.isNotNull(teacher) || 		
				Validator.isNotNull(parent) || 		
				Validator.isNotNull(school) || 		
				Validator.isNotNull(others) || 		
				Validator.isNotNull(collOther) ||  		
				Validator.isNotNull(staff) || 		
				Validator.isNotNull(college) || 		
				Validator.isNotNull(mainstay)){
			
			throw new DuplicateUserEmailAddressException();		
			
		}		
	}
	
	public static void screenName(ActionRequest req, String screenName)
			throws PortalException, SystemException {
		
		HttpServletRequest request = PortalUtil.getHttpServletRequest(req); 
		ThemeDisplay theme = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
		long companyId = theme.getCompanyId();
		
		screenName(companyId, screenName);
	}

		
	public static void screenName(long companyId, String screenName)
			throws PortalException, SystemException {

		if (Validator.isNull(screenName)) {
			throw new UserScreenNameException();
		}

		ScreenNameValidator screenNameValidator =
			(ScreenNameValidator)InstancePool.get(
				PropsValues.USERS_SCREEN_NAME_VALIDATOR);

		if (screenNameValidator != null) {
			if (!screenNameValidator.validate(companyId, screenName)) {
				throw new UserScreenNameException();
			}
		}

		if (Validator.isNumber(screenName) ) {

			throw new UserScreenNameException();
		}

		for (char c : screenName.toCharArray()) {
			if ((!Validator.isChar(c)) && (!Validator.isDigit(c)) &&
				(c != CharPool.DASH) && (c != CharPool.PERIOD) &&
				(c != CharPool.UNDERLINE)) {

				throw new UserScreenNameException();
			}
		}

		String[] anonymousNames = PrincipalBean.ANONYMOUS_NAMES;

		for (int i = 0; i < anonymousNames.length; i++) {
			if (screenName.equalsIgnoreCase(anonymousNames[i])) {
				throw new UserScreenNameException();
			}
		}
		User user = null;
		try{
		 user = UserLocalServiceUtil.getService().getUserByScreenName(companyId, screenName);
		}catch(NoSuchUserException ex){
			//
		}
		
		if (user != null) {
			throw new DuplicateUserScreenNameException();
		}

		String friendlyURL = StringPool.SLASH + screenName;
		Group group = null;
		try{
		 group = GroupLocalServiceUtil.getService().getFriendlyURLGroup(companyId, friendlyURL);
		}catch(NoSuchGroupException ge){
			//
		}
		
		if (group != null) {
			throw new DuplicateUserScreenNameException();
		}

		int exceptionType = LayoutImpl.validateFriendlyURL(friendlyURL);

		if (exceptionType != -1) {
			throw new UserScreenNameException(
				new GroupFriendlyURLException(exceptionType));
		}

		String[] reservedScreenNames = PrefsPropsUtil.getStringArray(
			companyId, PropsKeys.ADMIN_RESERVED_SCREEN_NAMES,
			StringPool.NEW_LINE, PropsValues.ADMIN_RESERVED_SCREEN_NAMES);

		for (int i = 0; i < reservedScreenNames.length; i++) {
			if (screenName.equalsIgnoreCase(reservedScreenNames[i])) {
				throw new ReservedUserScreenNameException();
			}
		}
	}
	
	public static void friendlyURL(String friendlyURL)
		throws PortalException, SystemException {

		if (Validator.isNull(friendlyURL)) {
			return;
		}
	
		int exceptionType = LayoutImpl.validateFriendlyURL(friendlyURL);
	
		if (exceptionType != -1) {
			throw new GroupFriendlyURLException(exceptionType);
		}				
	}

	public static void groupNameFormat(String name)
		throws PortalException, SystemException {

		if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
			(name.indexOf(StringPool.COMMA) != -1) ||
			(name.indexOf(StringPool.STAR) != -1)) {
			throw new GroupNameException();
		}		
	}
	
	public static void groupName(long companyId, String CommunityName)
		throws PortalException, SystemException{
		
		Group group = null;
		try{
		group = GroupServiceUtil.getGroup(companyId, CommunityName);
		}catch(NoSuchGroupException nsge){
			//
		}
		
		if(Validator.isNotNull(group)) throw new GroupNameException();
		
	}
	
	
}
